package com.ybw.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ybw.parseJson.YbwMethod;
import com.ybw.parseJson.YbwResult;
import com.ybw.parseJson.YbwUrl;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

public class ExcelUtil {
    public static List<YbwResult> ybwResults1() {
        try {
            File file = new File("C:\\COC1.1-prod\\JSON2.0\\src\\main\\resources\\data.json");
            FileInputStream fis = new FileInputStream(file);
            byte bytes[] = null;
            int length = fis.available();
            bytes = new byte[length];
            fis.read(bytes);
            fis.close();
            String str = new String(bytes, StandardCharsets.UTF_8);

            Map map1 = JSON.parseObject(str, Map.class);
            Map map2 = (Map) map1.get("paths");
            //map2是个map，键是url-prefix,值是一坨方法的集合(YbwUrl)

            //根据map2获取YbwUrl列表，并填充每个列表对象的path属性
            List<YbwUrl> ybwUrls = new ArrayList<>();
            for (Object obj : map2.entrySet()) {
                //obj是一个url:YbwUrl对象
                Map.Entry entry = (Map.Entry) obj;
                String temp1 = String.valueOf(JSON.toJSON(entry.getKey()));
                JSONObject temp2 = (JSONObject) JSON.toJSON(entry.getValue());
                YbwUrl ybwUrl = JSON.toJavaObject(temp2, YbwUrl.class);
                ybwUrl.setPath(temp1);
                ybwUrls.add(ybwUrl);
            }

            //根据YbwUrl列表，生成YbwResult列表
            List<YbwResult> ybwResults = new ArrayList<>();
            for (YbwUrl ybwUrl : ybwUrls) {
                YbwResult ybwResult = null;
                if (ybwUrl.getPut() != null) {
                    ybwResult = new YbwResult();
                    YbwMethod ybwMethod = ybwUrl.getPut();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("put");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if (ybwUrl.getGet() != null) {
                    ybwResult = new YbwResult();
                    YbwMethod ybwMethod = ybwUrl.getGet();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("get");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if (ybwUrl.getPost() != null) {
                    ybwResult = new YbwResult();
                    YbwMethod ybwMethod = ybwUrl.getPost();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("post");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
                if (ybwUrl.getDelete() != null) {
                    ybwResult = new YbwResult();
                    YbwMethod ybwMethod = ybwUrl.getDelete();
                    ybwResult.setPath(ybwUrl.getPath());
                    ybwResult.setMethod("delete");
                    ybwResult.setController(ybwMethod.getTags().get(0));
                    ybwResult.setDescription(ybwMethod.getSummary());
                    ybwResults.add(ybwResult);
                }
            }
            //path中所有路径参数改成统一正则表达式[a-zA-Z0-9]{0,64}
            int inta, intb;
            String stra, strb;
            for (YbwResult ybwResult : ybwResults) {
                if (ybwResult.getPath().contains("{")) {
                    inta = ybwResult.getPath().indexOf("{");
                    intb = ybwResult.getPath().indexOf("}");
                    stra = ybwResult.getPath().substring(0, inta);
                    if (intb + 1 < ybwResult.getPath().length()) {
                        strb = ybwResult.getPath().substring(intb + 1, ybwResult.getPath().length() - 1);
                    } else {
                        strb = "";
                    }
                    ybwResult.setPath(stra + "[a-zA-Z0-9]{0,64}" + strb);
                }
            }

            //根据controller分组
            Map<String, List<YbwResult>> map3 = ybwResults.stream().collect(Collectors.groupingBy(YbwResult::getController));
            //根据controller排序
            Map<String, List<YbwResult>> map4 = new TreeMap<>(
                    new Comparator<String>() {
                        @Override
                        public int compare(String o1, String o2) {
                            return Integer.valueOf(o1.compareTo(o2));
                        }
                    }
            );
            for (Object obj : map3.entrySet()) {
                Map.Entry<String, List<YbwResult>> entry = (Map.Entry) obj;
                map4.put(entry.getKey(), entry.getValue());
            }
            List<YbwResult> ybwResults1 = new ArrayList<>();
            for (Object obj : map4.entrySet()) {
                Map.Entry<String, List<YbwResult>> entry = (Map.Entry) obj;
                ybwResults1.addAll(entry.getValue());
            }
            return ybwResults1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
